﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;

namespace TaxiDispatchClient
{
    public partial class CallPassengerForm : DevExpress.XtraEditors.XtraForm
    {
        public CallPassengerForm(string phoneNumber)
        {
            InitializeComponent();
            _isLoad = true;
            textEdit_phoneNumber.Text = "9"+phoneNumber;
            SetPortNameValues();
           // MainForm.PhoneModem.ShowCallIdentification();
        }

        public CallPassengerForm()
        {
            InitializeComponent();
            _isLoad = true;
            textEdit_phoneNumber.Text = "9";
            SetPortNameValues();
           /// MainForm.PhoneModem.ShowCallIdentification();
        }


        private void CallPassengerForm_Load(object sender, EventArgs e)
        {
            _thread = new Thread(BindData);
            _thread.Start();
        }

        private Thread _thread;
        private bool _isLoad;

        private void BindData()
        {
            string timeStart = DateTime.Now.ToString("yyyy-MM-dd ") + "00:00:00";
            string timeEnd = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string sqlWhere = "(type='0' or type ='1') and calltime between '" +             
                timeStart + "' and '" + timeEnd + "'";
            DataSet ds = MainForm.PositionService.CallRecordsGetList(sqlWhere);
            if(ds!=null)
            {
                BeginInvoke(new MethodInvoker(() => { 
                    gridControl1.DataSource = ds.Tables[0];
                    gridView1.GroupPanelText = "今日通话记录  共"+ds.Tables[0].Rows.Count+"条";
                }));
                //gridControl1.DataSource = ds.Tables[0];
            }
        }


        private  void SetPortNameValues()
        {
            comboBoxEdit_Comlist.Properties.Items.Clear();            
            foreach (string str in SerialPort.GetPortNames())
            {
                comboBoxEdit_Comlist.Properties.Items.Add(str);
            }
            if (comboBoxEdit_Comlist.Properties.Items.Count > 0)
            {
                comboBoxEdit_Comlist.SelectedItem = Service.PhoneModemCom.serialPort.PortName;
            }
            else
            {
                comboBoxEdit_Comlist.Enabled = false;
                simpleButton_modify.Enabled = false;
                simpleButton_call.Enabled = false;
                lb_noComs.Visible = true;
            }
        }

        private void simpleButton_modify_Click(object sender, EventArgs e)
        {
            if(simpleButton_modify.Text=="更改")
            {
                comboBoxEdit_Comlist.Enabled = true;
                simpleButton_modify.Text = "保存";
                simpleButton_cancel.Visible = true;
            }
            else
            {
                string newCom = comboBoxEdit_Comlist.Properties.Items[comboBoxEdit_Comlist.SelectedIndex].ToString();
                if (MainForm.PhoneModem.ChangeCom(newCom))
                {
                    comboBoxEdit_Comlist.Enabled = false;
                    simpleButton_modify.Text = "更改";
                    simpleButton_cancel.Visible = false;    
                }
                else
                {
                    XtraMessageBox.Show("更改失败！");
                }
            }
        }

        private void simpleButton_cancel_Click(object sender, EventArgs e)
        {            
            comboBoxEdit_Comlist.SelectedItem = Service.PhoneModemCom.serialPort.PortName;
            comboBoxEdit_Comlist.Enabled = false;
            simpleButton_modify.Text = "更改";
            simpleButton_cancel.Visible = false;

        }

        private void simpleButton_call_Click(object sender, EventArgs e)
        {
            if(textEdit_phoneNumber.Text.Trim().Length<6)
            {
                XtraMessageBox.Show("请输入电话号码！");
                return;
            }           
            if (MainForm.PhoneModem.CallPhoneNumber(textEdit_phoneNumber.Text))
            {
                textEdit_phoneNumber.Enabled = false;
                simpleButton_call.Enabled = false;
                simpleButton_break.Enabled = true;
                simpleButton_cleartext.Enabled = false;
                CallRecordsInfo model = new CallRecordsInfo();
                string phoneNum = textEdit_phoneNumber.Text;
                if(phoneNum[0]=='9')
                {
                    phoneNum=phoneNum.Substring(1);
                }
                model.PhoneNumber = phoneNum;
                model.Type = 1;
                model.CallTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                MainForm.PositionService.CallRecordsAdd(model);
                //TradeInfo trade = new TradeInfo();
                //trade.PhoneNumber = "07922252222";
                //trade.PhoneNumber2 = phoneNum;
                //trade.Type = 2;
                //trade.TradeTime = DateTime.Now;
                //MainForm.PositionService.AddTradeInfo(trade);

                _thread = new Thread(BindData);
                _thread.Start();
            }
            else
            {
                XtraMessageBox.Show("拨号失败，请检查设备！");
            }
        }

        private void simpleButton_break_Click(object sender, EventArgs e)
        {
            if(MainForm.PhoneModem.HangUp())
            {
                textEdit_phoneNumber.Enabled  = true;
                simpleButton_call.Enabled = true;
                simpleButton_break.Enabled = false;
                simpleButton_cleartext.Enabled = true;
            }
        }

        private void simpleButton_cleartext_Click(object sender, EventArgs e)
        {
            textEdit_phoneNumber.Text = "";

        }

        #region  拨号按键

        private void ButtonClick(SimpleButton simple)
        {            
            textEdit_phoneNumber.Text += simple.Text;
        }

        private void simpleButton_1_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_1);
        }

        private void simpleButton_2_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_2);
        }

        private void simpleButton_3_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_3);
        }

        private void simpleButton_4_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_4);
        }

        private void simpleButton_5_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_5);
        }

        private void simpleButton_6_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_6);
        }

        private void simpleButton_7_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_7);
        }

        private void simpleButton_8_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_8);
        }

        private void simpleButton_9_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_9);
        }

        private void simpleButton_0_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_0);
        }

        private void simpleButton_star_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_star);
        }

        private void simpleButton_sharp_Click(object sender, EventArgs e)
        {
            ButtonClick(simpleButton_sharp);
        }
#endregion


        #region 窗体效果
        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;
            Cursor = Cursors.Arrow;
        }
        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            if(simpleButton_break.Enabled)
            {
                MainForm.PhoneModem.HangUp();
            }
            Close();
        }


        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void CallPassengerForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.Close();
            }
        }

        #endregion

        private void CallPassengerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(_thread !=null)
            {
                _thread.Abort();
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if(row!=null)
            {
                string phoneNumber = row["phoneNumber"].ToString();
                textEdit_phoneNumber.Text = "9" + phoneNumber;
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (row != null)
            {
                if (_isLoad)
                {
                    _isLoad = false;
                    return;
                }
                string phoneNumber = row["phoneNumber"].ToString();
                textEdit_phoneNumber.Text = "9" + phoneNumber;
            }
        }

    }   
}